class DataCollectionController < ApplicationController
	layout 'standard'

	def list
		@show_yls = false
		
    sort = case params[:sort]
    when "target_date" then "target_date"
    when "youth_id" then "youth_id"
    when "type" then "data_collection_types.name"
    when "status" then "status"
    when "institution" then "institutions.name"
    when "data_collector" then "data_collectors.name"
    when "target_date_reverse" then "target_date DESC"
    when "youth_id_reverse" then "youth_id DESC"
    when "type_reverse" then "data_collection_types.name DESC"
    when "status_reverse" then "status DESC"
    when "institution_reverse" then "institutions.name DESC"
    when "data_collector_reverse" then "data_collectors.name DESC"
		end
		
		if (sort.nil?)
			sort = "target_date"
		end
		
		if (sort.include?("status"))
			status_sort = sort
			sort = nil
		end
		
    @data_collections = DataCollection.find(:all, :include => ['data_collection_type', 'youth', {:youth => 'institution'}, {:youth => :data_collector}],
      :order => sort, :conditions => params[:data_collection])
    
		unless (status_sort.nil?)
			@data_collections = @data_collections.sort_by{|dc| [dc.calculate_status]} if (status_sort.include?("status"))
			@data_collections.reverse! if (status_sort == "status DESC")
		end

		
		# Doing some manual filtering of non-database values.
		# Ideally, these should be calculated into the SQL-query to save processing time, but it's complicated.
		
		@data_collections_copy = @data_collections.clone
		
		unless (params[:data_collection_status].nil?)
			@data_collections_copy.each do |dc| 
				match = false
				params[:data_collection_status].each do |s|
					match = true if (dc.calculate_status == s)
				end
        @data_collections.delete(dc) unless match
			end
		end
		
		if request.xml_http_request?
			render :partial => "data_collections_list", :layout => false
		end
	end


	def new
		@data_collection = DataCollection.new
	end

  
	def create
		@data_collection = DataCollection.new(params[:data_collection])
		
		@data_collection.save
    if @data_collection.errors.empty?
      redirect_to :controller => 'youth', :action => 'edit', :id => @data_collection.youth_id
		else
			render :text => "OUCH!"
		end
	end

  
	def edit
		@data_collection = DataCollection.find(params[:id])
		@questionnaires = @data_collection.questionnaires
	end

	
	def update
		@data_collection = DataCollection.find(params[:id])
		@data_collection.update_attributes(params[:data_collection])

    if @data_collection.errors.empty?
      #redirect_to :action => 'edit', :id => @data_collection, :changesSaved => true
      flash[:notice] = get_changes_saved_text(@data_collection)
      redirect_to :back
    else
      @institution.errors.each_full do |e|
        flash[:error] = e
      end
		end
		
		
	end


  def delete
    @data_collection = DataCollection.find(params[:id])
    @data_collection.destroy

    redirect_to :controller => 'youth', :action => 'edit', :id => @data_collection.youth_id
  end

end
